"""浏览器的配置和操作的封装
"""
from typing import Any, Optional

from selenium import webdriver
from selenium.webdriver.common.by import By


class EdgeBrowser:

    """_summary_"""

    def __init__(self) -> None:
        self.user: Optional[Any] = None
        self.dst: Optional[Any] = None
        self.task: Optional[Any] = None
        self.res: Optional[Any] = None
        self.upload_flag: Optional[Any] = None

        edge_options = webdriver.EdgeOptions()
        # edge_options.use_chromium = True
        edge_options.use_webview = True
        edge_options.add_experimental_option("detach", True)
        # edge_options.add_argument("--headless")
        self.driver = webdriver.Edge(options=edge_options)
        # 最大化窗口
        self.driver.maximize_window()

    def open(self, url):
        """_summary_

        Args:
            url (_type_): _description_
        """
        self.driver.get(url)

    def close(self):
        self.driver.quit()  # 关闭浏览器窗口并退出驱动程序

    def find_ele_by_id(self, html_id):
        return self.driver.find_element(By.ID, html_id)  # 找到输入框元素

    def click_ele_by_id(self, html_id):
        dst_ele = self.driver.find_element(By.ID, html_id)  # 找到输入框元素
        dst_ele.click()

    def fill(self):
        # 增加工作记录元素
        add_work_ele = self.driver.find_element(By.ID, "ctl00_TreeView1t2")
        add_work_ele.click()  # 在输入框中输入文本

        # 项目选项
        pro_ele = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_DropDownList1")
        pro_options = pro_ele.find_elements(By.TAG_NAME, "option")
        for option in pro_options:
            if option.get_attribute("value") == "5. 工事":
                option.click()
                break

        # 人员选项
        user_ele = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_DropDownList20")
        user_ele.click()  # 在输入框中输入文本
        # 获取所有option元素
        user_options = user_ele.find_elements(By.TAG_NAME, "option")
        for option in user_options:
            if option.get_attribute("value") == self.user:
                option.click()
                break

        # 目标输入框
        input_element = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_TextBox22")
        input_element.send_keys(self.dst)

        # 任务输入框
        input_element = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_TextBox23")
        input_element.send_keys(self.task)

        # 结果输入框
        input_element = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_TextBox24")
        input_element.send_keys(self.res)

        # # 完成并上传
        if self.upload_flag == "是":
            input_element = self.driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_Button20")
            input_element.click()
